package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.adapter.BankSupportV2Adapter;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class BankSupportActivity extends BaseActivity {
    @BindView(R.id.left_img)
    ImageView leftImg;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    private Unbinder unbinder;
    private BankSupportV2Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_list_support);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        adapter = new BankSupportV2Adapter(this);
        list.setAdapter(adapter);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getBankList();
            }
        });
        getBankList();
    }

    private void getBankList() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        refresh.setRefreshing(true);
        Api.api_service.getBankSupport().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.update(result.data);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick(R.id.left_img)
    public void onViewClicked() {
        finish();
    }
}
